package com.syg.configuration;

import com.syg.boot.core.common.SpringContextHelper;
import com.syg.dao.mapper.TclassMapper;
import com.syg.dao.service.TclassService;
import com.syg.service.SqlLocalService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.IOException;

/**
 * @author ps
 */
@Configuration
@Slf4j
@EnableConfigurationProperties({SqlProperties.class})
public class SqlAutoConfiguration {


    private SqlProperties sqlProperties;

    @Value("${spring.datasource.write.driver-class-name}")
    private String driverClassName;

    public static final String MYSQL = "MYSQL";
    public static final String ORACLE = "ORACLE";

    private final String classMapperLocation = "classpath:mapper/TclassMapper.xml";


    /**
     * 不声明@Autowired,因为只有一个构造方法,spring会尝试查找需要的参数
     * @param sqlProperties
     */
    public SqlAutoConfiguration(SqlProperties sqlProperties) {
        this.sqlProperties = sqlProperties;
    }


    /**
     * @date
     * @apiNote 生成bizService
     */
    @DependsOn("springContextHelper")
    @Bean(name = "tclassService")
    public TclassService tFscBizJoinHisService() {
        TclassService classService = null;
        try {
            org.apache.ibatis.session.Configuration configuration = parseXmlMapper(classMapperLocation);
            classService = new TclassService(configuration.getMapper(TclassMapper.class, SpringContextHelper.getBean(SqlSessionTemplate.class)));
        } catch (Exception e) {
            log.error("flow client: init bean failure! bean name is [tFscBizJoinHisService] !", e);
        }
        return classService;
    }


    @DependsOn("tclassService")
    @Bean(name = "sqlLocalService")
    public SqlLocalService flowLocalService() {
        return new SqlLocalService();
    }


    private org.apache.ibatis.session.Configuration parseXmlMapper(String mapper) throws IOException {
        org.apache.ibatis.session.Configuration configuration = SpringContextHelper.getBean(SqlSessionFactory.class).getConfiguration();
        // step.1 扫描文件
        Resource resource = new PathMatchingResourcePatternResolver().getResource(mapper);
        // step.2 加载mapper文件
        XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(resource.getInputStream(), configuration, resource.toString(), configuration.getSqlFragments());
        xmlMapperBuilder.parse();
        log.info("^_^加载sql mapper文件[" + resource.getFilename() + "]成功^_^");
        return configuration;
    }
}
